/*********************************************************
          File Name:9.43.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 15 Sep 2015 09:48:21 AM CST
**********************************************************/

#include <iostream>
#include <string>
using namespace std;

void subxx(string &s, const string &oldVal, const string &newVal)
{
        for(auto it = s.begin(); it != s.end(); )
        {
                decltype(oldVal.length()) i =0;
                while(*it == oldVal[i])
                {
                        ++it;
                        ++i;
                }
                it -= i;
                if(i >= oldVal.length())        // i length including '\0'
                {
                        it = s.erase(it, it+oldVal.size());
                        it = s.insert(it, newVal.begin(), newVal.end());
                        it += newVal.size();
                }
                else
                        ++it;
        }
}

int main()
{
        string s="tho tho tho, thru thru thru";
        cout << s << endl;

        subxx(s, "tho", "though");
        subxx(s, "thru", "through");

        cout << s << endl;

        return 0;
}
